using BlazorAdmin.Models;
using BlazorAdmin.Interface;
using BootstrapBlazor.Components;
using BlazorAdmin.Web.Shared.Components;
using BlazorAdmin.Web.Shared.Models;
using BlazorAdmin.Web.Shared.Services;
using Microsoft.AspNetCore.Components;

namespace BlazorAdmin.Web.Shared.Pages.Admin
{
    public partial class SysSettings
    {
        public ITableSearchModel TableSearchModel { get; } = new SettingsSearchModel();

        protected override void OnInitialized()
        {
            base.OnInitialized();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        Task<QueryData<Setting>>? OnQueryAsync(QueryPageOptions options) =>
            SettingsService.GetSettingsList(new(options, AppContext?.LoginName));

        /// <summary>
        /// 保存
        /// </summary>
        async Task<bool> OnSaveAsync(Setting item, ItemChangedType type)
        {
            var errMsg = await SettingsService.SaveSettings(item);

            if (string.IsNullOrEmpty(errMsg))
            {
                return true;
            }
            else
            {
                MsgService?.Show(new()
                {
                    Icon = "fa-solid fa-circle-info",
                    Content = errMsg,
                    Color = Color.Danger
                });

                return false;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        Task<bool> OnDeleteAsync(IEnumerable<Setting> items) =>
            SettingsService.DeleteSettings(items.Select(t => t.ID).ToList());

        /// <summary>
        /// 查看详情
        /// </summary>
        Task ShowDetail(Setting item)
        {
            Dictionary<string, object?>? paramater = new();
            paramater.Add("Value", item);

            var option = new DialogOption()
            {
                Size = Size.Large,
                Title = "查看详情",
                BodyContext = item,
                Component = BootstrapDynamicComponent.CreateComponent<SettingsDetail>(paramater)
            };

            return DialogService.Show(option);
        }

        #region Items 字典数据

        #endregion

        #region Inject 注入的服务

        [Inject]
        private BootstrapAppContext? AppContext { get; set; }

        [Inject]
        private DialogService? DialogService { get; set; }

        [Inject]
        private MessageService? MsgService { get; set; }

        [Inject]
        private ISettings? SettingsService { get; set; }

        #endregion
    }
}
